var js = {};
js.underas = {};
js.underas.core = {};
js.underas.container = {};
js.underas.controller = {};
js.underas.net = {};
js.underas.util = {};
/**
 * @class js.underas.core.Underas
 */
js.underas.core.Underas = {		
		"uid":Number.random(500, 1500),
		"_cache":true,
		/**
		* habilita e desabilita o cache para os modulos.
		*
		* @example
		* js.underas.core.Underas.cache(false);
		*/
		"cache":function(p_on){
			this._cache = p_on;
		},
		/**
		* incrementa e retorna o proximo uid do componente a ser criado dinamicamente.
		*
		* @example
		* js.underas.core.Underas.getNextUid();
		* @returns {Number}
		*/
		"getUid":function(){
			this.uid++;
			return this.uid;
		},
		/**
		* Retorna o ultimo uid usado para um componente a ser criado dinamicamente.
		*
		* @example
		* js.underas.core.Underas.getLastUid();
		* @returns {Number}
		*/
		"getLastUid":function(){
			return this.uid;
		},
		/**
		* Retorna o proximo uid do proximo componente a ser criado dinamicamente.
		*
		* @example
		* js.underas.core.Underas.getNextUid();
		* @returns {Number}
		*/
		"getNextUid":function(){
			return this.uid+1;
		},
		/**
		* Carrega um modulo na memoria dinamicamente.
		*
		* @param {Object} args
		* @example
		* js.underas.core.Underas.loadModule({mod:"test.view.Fool"});
		*/
		"loadModule":function(args){
			var configs = Object.merge({"obj":"","mod":"","url":"","act":"","p":[],"puid":"","icon":"file","title":"cadastro"},args);		
			configs.extmod = configs.mod;
			if(configs.url==""){
				//nao tem url padrao usar o modulo
				configs.url = configs.mod.replace(/\./g,"/")+".js";				
				var listmods = configs.mod.split("."); 
				var tmm = listmods.length;
				configs.extmod = listmods[tmm-1];
				if(configs["obj"]==""){
					configs["obj"]=configs.extmod.toLowerCase();
				}
			}			
			var nextUidMod = "modview_"+this.getNextUid();
			//o modulo ja foi carregado
			if(window[configs["obj"]]){
				if(window[configs["obj"]].getEle){
					nextUidMod = "modview_"+window[configs["obj"]].getEle().get("data-modview");
					window[nextUidMod].show(true);
					//verifica se existe funcao para executar
					if(configs["act"]!=""){
						window[configs["obj"]][configs["act"]].apply(window[configs["obj"]],configs["p"]);
					}else if(configs["embed"]!=""&&window[configs["obj"]]._mainlist&&configs["puid"]!=""){
						var tmpSelectedItem = window[configs["puid"]].getMainList().getSelectedItem();
						if(tmpSelectedItem){
							window[configs["obj"]].getMainList().setDataProvider(tmpSelectedItem[configs["embed"]]);
						}else{
							window[configs["obj"]].getMainList().setDataProvider([]);
						};					
						window[configs["obj"]].getMainList().changeToIndex(0);
					};
				};
			}else{
				//nao foi carrregado ainda
				//inicializa o objeto
				window[configs["obj"]] = {};				
				if(configs["act"]!=""){
					window[configs["obj"]][configs["act"]]=function(){};					
				};
				if(configs["puid"]==""){
				//nao foi carrregado ainda e nao tem um main module
					window[nextUidMod] = new js.underas.container.ModView(configs["title"]);
					$$("#conteudo").grab(window[nextUidMod].getEle());
					nextUidMod = "modview_"+window[nextUidMod].uid;
					window[nextUidMod].setIcon(configs["icon"]);
				}else{
				//tem um main module
					nextUidMod = "modview_"+window[configs["puid"]].getEle().get("data-modview");
				};
				if(typeof configs["onLoad"] == "undefined" || typeof configs["onLoad"] == "null"){
					configs["onLoad"] = function(){					
						window[configs["extmod"]].prototype._varmod = configs['obj'];						
						window[configs["obj"]] = new window[configs["extmod"]](); 						
						window[configs["obj"]].setUrlModule(configs["url"]);
						window[configs["obj"]].setVarModule(configs["obj"]);
						window[configs["obj"]].setDsModule();
						window[configs["obj"]].getEle().addClass(configs["extmod"]).set("data-modvar",configs["obj"]);						
						window[nextUidMod].append(window[configs["obj"]]);
						window[nextUidMod].show(true);
						window[configs["obj"]]._onStart();
						if(configs["embed"]!=""){
							window[configs["obj"]]._embedItem = configs["embed"];
							window[configs["obj"]]._embedFather = configs["puid"];
						};
						if(window[configs["obj"]]._maintoolbar){
							window[configs["obj"]][window[configs["obj"]]._maintoolbar].activate(true);
							if(window[configs["obj"]]._mainlist){
								window[configs["obj"]].getMainList().changeToIndex(0);
							};							
						};
						window[configs["obj"]].onStart();	

						if(configs["act"]!=""&&configs["act"]){
							window[configs["obj"]][configs["act"]].apply(window[configs["obj"]],configs["p"]);  
						}else if(configs["embed"]!=""&&window[configs["obj"]]._mainlist&&configs["puid"]!=""){
							var tmpSelectedItem = window[configs["puid"]].getMainList().getSelectedItem();
							if(tmpSelectedItem){
								window[configs["obj"]].getMainList().setDataProvider(tmpSelectedItem[configs["embed"]]);
							}else{
								window[configs["obj"]].getMainList().setDataProvider([]);
							};
							window[configs["obj"]].getMainList().changeToIndex(0);
						};
					};
				};
				if(!this._cache){
					configs["url"]+="?a"+Number.random(5500, 8500)+"="+Number.random(5500, 8500);
				};
				if(!window[configs["extmod"]]){
					Asset.javascript(configs["url"], {
						id: 'mod_'+configs["obj"],
						onLoad:configs["onLoad"]
					}); 
				}else{
					configs["onLoad"]();
				};

			}
		},
		/**
		* Retorna o valor do paramentro dentro da location da janela.
		*
		* @example
		* js.underas.core.Underas.getUrlParam("key_in_url");
		* @param {String} p_name 
		* @returns {String}
		*/		
		"getUrlParam":function(p_name){
			var results = new RegExp('[\?&]' + p_name + '=([^&#]*)').exec(window.location.href);
			if (results==null){
			   return "";
			}else{
			   return results[1] || "";
			}	
		},
		/**
		* Imprimi um relatorio.
		*
		* @example
		* js.underas.core.Underas.printDataProvider({report_json_data},"test/report_test.json");
		* @param {Object} p_dta 
		* @param {String} p_url_template
		*/	
		"printDataProvider":function(p_dta,p_url_template){			
			js.underas.core.Underas.loadModule({"act":"loadReportPreview","p":[p_dta,p_url_template],"mod":"lib.org.underas.util.PdfPreview","title":"relatorios","icon":"print"});			
		},
};
